<template>
<div>
  <h1 style="background-color: #42b983">添加新闻</h1>
  <p>标题<input type="text" v-model="from.title"></p>
  <p>内容<input type="text" v-model="from.content"></p>
  <button @click="add">添加新闻</button>
  <br>
  <h1 style="background-color: crimson">展示新闻</h1>
  <table>
    <tr>
      <td>标题</td>
      <td>内容</td>
    </tr>
    <tr v-for="i in news">
      <td>{{i.title}}</td>
      <td>{{i.content}}</td>
    </tr>
  </table>
</div>
</template>

<script>
export default {
  name: "add_news",
  data(){
    return{
      from:{},
      news:[],
    }
  },
  methods:{
    add(){
      this.$axios.post('users/news/',this.from)
        .then(res=>{
          if(res.data.code==200){
            alert('添加成功')
          }else {
            alert('添加失败')
          }
        })
    },
    get_news(){
      this.$axios.get('users/news/')
        .then(res=>{
            if(res.data.code==200){
              this.news = res.data.list
              this.get_news()
            }
        })
    }
  },
  mounted() {
    this.get_news()
  }
}
</script>

<style scoped>

</style>
